<template>
    <div>
     <CityHeader></CityHeader>
     <CitySearch :cities='cities'></CitySearch>
     <CityList
      :cities="cities"
      :hot="hotCities"
      :letter="letter"
     ></CityList>
    <!-- 监听子组件触发的事件 -->
     <CityAlphabet :cities="cities" @change="LetterChange"></CityAlphabet>
    </div>
</template>

<script>
import axios from 'axios'
import CityHeader from './components/Header'
import CitySearch from './components/Search'
import CityList from './components/List'
import CityAlphabet from './components/Alphabet'

export default {
  name: 'City',
  data () {
    return {
      cities: {},
      hotCities: [],
      // 父组件通过属性子组件传值
      letter: ''
    }
  },
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet
  },
  methods: {
    getCityInfo () {
      axios.get('/api/city.json').then(res => {
        this.handleGetCityInfoSucc(res)
      })
    },
    handleGetCityInfoSucc (res) {
      // eslint-disable-next-line no-redeclare
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.cities = data.cities
        this.hotCities = data.hotCities
      }
    },
    // 定义向list传值
    LetterChange (letter) {
      this.letter = letter
    }
  },
  mounted () {
    this.getCityInfo()
  }
}
</script>

<style lang="stylus" scoped>
</style>
